 #include "stdafx.h"
 using namespace std;

Mod_ETP::Mod_ETP(Mod_in *iinp):Mod_out_handling(iinp)
{
	;
}
Mod_ETP::~Mod_ETP  ()
{;}
int Mod_ETP::compute(time_intervall &ti){}
Mod_ETP_Turc::Mod_ETP_Turc(map <string,int> &iIO_options,Mod_in *iinp,
						   Inp_weather *iweather,
                           Mod_Interception *iimod_interception,
                           Mod_Crop *iimod_crop,
                           Mod_wc *iimod_Wc,
                           Mod_Surface_water *iimod_Surface_water,
                           Mod_Snow *iimod_Snow):Mod_ETP(iinp)
{
    //1. switches setzen
    IO_options=iIO_options;
    //2. Input_objekt setzen

    //3. Out setzen
    
    
    //4. Modelle verknüpfen
    inp_weather= iweather;
    mod_interception=iimod_interception;
    mod_crop=iimod_crop;
    mod_wc=iimod_Wc;
    mod_Surface_water=iimod_Surface_water;
    mod_snow=iimod_Snow;
    
    //5. Output_kategorien für diese Variante setzen!!
    out->fluxes.set_key("ETP_Interzeption");
    out->fluxes.set_key("ETP_Transpiration");
    out->fluxes.set_key("ETP_Evaporation");
    out->fluxes.set_key("ETP_Snowevaporation");
    out->fluxes.set_key("ETP_Surface_water_evaporation");
    out->fluxes.set_key("ETP");
    timepointmap <double> null_timepointmap;
    out->states["Test"]=null_timepointmap;
            
}
Mod_ETP_Turc::~Mod_ETP_Turc(){;}
int Mod_ETP_Turc::compute(time_intervall &ti)
{
    double T;
	double R;
	double ETP;
        int dummy;
	T=this->inp_weather->get_rate("T",ti);
        R=this->inp_weather->get_rate("R",ti);
        // r conversion
        R=R/100/100*60*60*24;
        // ETP Rate mm/day
        ETP= 0.0031*((T/(T+15))*(R+209));//(beachte evtl. Korrekturfaktor Cf!!)
        dummy=(ETP<0)+2*(ETP>7);
        switch (dummy)
        {
                case 1: ETP=0.1;break;
                case 2: ETP=7;break;
                default:break;
        }
        distribute_ETP (ETP,ti);
        return 0;
}
int Mod_ETP_Turc::distribute_ETP (double ETP,time_intervall &ti)
{
    timestamp start=ti.get_start();
    timestamp end=ti.get_end();
    
    double pot_ETP_interzept=this->mod_interception->get_states("Interception_Storage",start);
    double pot_ETP_transpiration=this->mod_crop->get_states ("pot_TP",start);
    double pot_ETP_surface_water=this->mod_Surface_water->get_states("Surface_Storage",start);
    double pot_ETP_snow=this->mod_snow->get_states("Snow_equivalent",start);
    double pot_ETP_evaporation=this->mod_wc->get_states("pot_EP",start);
    double whole_ETP=ETP;
    ETP=max(0.0,ETP-pot_ETP_interzept);this->set_push_back_fluxes("ETR_Interception",ti,min(pot_ETP_interzept,ETP));
    ETP=max(0.0,ETP-pot_ETP_transpiration);this->set_push_back_fluxes("ETR_Transpiration",ti,min(pot_ETP_transpiration,ETP));
    ETP=max(0.0,ETP-pot_ETP_surface_water);this->set_push_back_fluxes("ETR_Surface_water",ti,min(pot_ETP_surface_water,ETP));
    ETP=max(0.0,ETP-pot_ETP_snow);this->set_push_back_fluxes("ETR_Snow",ti,min(pot_ETP_snow,ETP));
    ETP=max(0.0,ETP-pot_ETP_evaporation);this->set_push_back_fluxes("ETR_Evaporation",ti,min(pot_ETP_evaporation,ETP));
    whole_ETP=whole_ETP-ETP;
    this->set_push_back_fluxes("ETP",ti,whole_ETP);
    return 0;
}

// double mod_ETP::get_fluxes(string key, time_intervall &ti)
// {
//     if (out->fluxes->get_size()>0)
//     {
//         if(out->fluxes->get_last_time_intervall>ti)
//         {
//             return out->fluxes->get_rate (key,ti);
//         }
//     }
//     compute_loop(ti);
//     return out->fluxes->get_rate(key,ti);
// }
// double mod_ETP::get_states(string key,timestamp &ts)
// {
//     if (find(out->states->begin(),out->states->end(),key)==out->states->end())
//     {
//         err::err3("mod_ETP::get_states:No such key in states");
//     }
//     if(out)
// }
// int mod_ETP::set_fluxes(string key,time_intervall &ti, double wert)
// {
//     out->fluxes->set_push_back_val(key,time_intervall &ti, wert);
// }
// int mod_ETP::compute_loop(time_intervall &ti)
// {
//	//Berechnet ETP_ergebnisse und speichert es in out
//	map <string,double> zwi;
//        int row, maxrow;
//        //1. check
//        if(ti<=out->fluxes->get_last_time_intervall())
//        {
//            err::err3("mod_ETP::compute_loop: the values for the timeintervall do already exist, therefore no need for computation");
//        }
//        //1. finden der position für die erste Berechnung
//        row=inp->tempgrid->get_pos_of_corresponding_time_intervall(out->fluxes->get_last_time_intervall());
//        if( (maxrow=inp->tempgrid->get_pos_of_last_overlapping_intervall(ti))==inp->tempgrid->get_size())
//        {
//            err::err3("mode_ETP::compute_loop: could not find the right timeintervall in tempgrid");
//        }
//        while (row<=maxrow)
//	{
//            this->compute(inp->tempgrid->get_time_intervall(row));
//		row++;
//	}
//	
//	return 0;
// }
 
